package im.yisme.model.game;


import im.yisme.view.ui.util.UIparams;
import im.yisme.vo.RawActionVO;
import android.graphics.PointF;

public class PackagedGameAction {

	
	private int action;
	private PointF start,end;
	private int startSection,endSection;
	private int moment,startMoment;
	
	
	public PackagedGameAction(int moment){
		this.setMoment(moment);
		startMoment = moment;
	}

	public float getDistance(float x, float y){		
		return (float) Math.hypot(x-end.x, y-end.y);
	}

	public boolean addAction(int action, float x, float y){
		
		this.action = action;
		
		if(action == RawActionVO.DOWN){
			end = new PointF(x, y);
			start = new PointF(x, y);
			startSection = inWhichSection(start);
			return true;
		}
		if(action == RawActionVO.MOVE){
			if(x == end.x && y == end.y) return false;
		}
		
		
		end.set(x, y);
		
		return true;
	}
	
	public int getAction() {
		return action;
	}
	
	public int getStartSection(){
		return startSection;
	}
	public int getEndSection(){
		
		float x = end.x - start.x;
		float y = end.y - start.y;
		
		if(Math.abs(x) >= Math.abs(y)){
			
			if(x >= 0){
				switch (startSection) {
				case 0:
					endSection = 1;
					break;
				case 1:
					startSection = 0;
					endSection = 1;
					break;
				case 2:
					endSection = 3;
					break;
				case 3:
					startSection = 2;
					endSection = 3;
					break;
				default:
					break;
				}
			}
			else {
				switch (startSection) {
				case 0:
					startSection = 1;
					endSection = 0;
					break;
				case 1:
					endSection = 0;
					break;
				case 2:
					startSection = 3;
					endSection = 2;
					break;
				case 3:
					endSection = 2;
					break;
				default:
					break;
				}
			}
			
		}
		else {
			
			if(y >= 0){
				switch (startSection) {
				case 0:
					endSection = 2;
					break;
				case 1:
					endSection = 3;
					break;
				case 2:
					startSection = 0;
					endSection = 2;
					break;
				case 3:
					startSection = 1;
					endSection = 3;
					break;
				default:
					break;
				}
			}
			else {
				switch (startSection) {
				case 0:
					startSection = 2;
					endSection = 0;
					break;
				case 1:
					startSection = 3;
					endSection = 1;
					break;
				case 2:
					endSection = 0;
					break;
				case 3:
					endSection = 1;
					break;
				default:
					break;
				}
			}
		}
		
		
		return endSection;
	}




	
	
	public int inWhichSection(PointF point){
		
		if(point.x < UIparams.screenWidth/2){
			
			if(point.y < UIparams.screenHeight/2)
				return 0;
			else
				return 2;
			
		}
		else {
			
			if(point.y < UIparams.screenHeight/2)
				return 1;
			else 
				return 3;
		}
		
	}

	public int getMoment() {
		return moment;
	}
	public int getStartMoment(){
		return startMoment;
	}

	public void setMoment(int moment) {
		this.moment = moment;
	}
	
}
